$import("com.joypard.platform.log.fnlog.FnLogDI");

/**
 * 定义日志视图
 */
K.defView("com.joypard.platform.log.fnlog.FnLogView", {
	/**
	 * 显示相关组件到界面
	 */
	render : function() {
		Ext.QuickTips.init();
		this.logSearchPanel = this.createLogSearchPanel();// 业务日志搜索面板
		this.logGridPanel = this.createLogGridPanel(); // 业务日志列表面板

		this.MP = new K.Ext.MainPanel({
			items : [ this.logSearchPanel, this.logGridPanel ]
		});
	},

	/**
	 * 创建日志搜索面板
	 */
	createLogSearchPanel : function() {
		var options = {
			title : "条件查询",
			region : "north",
			margins : "2 2 2 2",
			frame : true,
			iconCls : "p-search",
			collapsible : true,
			bodyCssCls : "search-gb",
			contentEl : "fnLog-search"
		};
		var me = this;

		$("#fnLog-search").keypress(function(evt) {
			var tagName = evt.target.tagName;
			if (tagName == "INPUT" && evt.keyCode == Ext.EventObject.ENTER) {
				me.onSearchLog();
			}
		});
		return new K.Ext.Panel(options);
	},
	/**
	 * 扩展行
	 */
	expander : new Ext.ux.grid.RowExpander({
		tpl : new Ext.Template('<p><b>业务日志:</b><br> {bizLog}</p><br>')
	}),
	/**
	 * 创建日志的列表面板
	 */
	createLogGridPanel : function() {
		var errorImage = "<img src='" + $res_path_resources + "/images/state/error.png" + "'  title='失败' />";
		var successImage = "<img src='" + $res_path_resources + "/images/state/success.png" + "' title='成功' />";
		var me = this;
		var options = {
			title : "日志列表",
			iconCls : "p-grid",
			margins : "0 2 2 2",
			region : "center",
			viewConfig : {
			// forceFit : true
			}, // 自动分配列宽
			plugins : me.expander,
			store : FnLogDI.createLogStore(),
			tbar : [ {
				text : "查询",
				iconCls : 'search',
				handler : function() {
					me.onSearchLog();
				}
			} ],
			columns : [ me.expander, {
				header : "状态",
				width : 55,
				align : 'center',
				dataIndex : "type",
				renderer : function(value) {
					if (value == "成功") {
						return successImage;
					} else {
						return errorImage;
					}
				}
			}, {
				header : "应用系统",
				width : 100,
				dataIndex : "appName"
			}, {
				header : "所属模块",
				width : 80,
				dataIndex : "moduleName",
				renderer : function(value, meta, record) {
					var moduleNamePath = record.get("moduleNamePath")
					if (moduleNamePath) {
						return "<span ext:qtip='模块路径【" + moduleNamePath + "】'>" + value + "</span>";
					}
					return value;
				}
			}, {
				header : "操作人",
				width : 100,
				dataIndex : "creator"
			}, {
				header : "操作时间",
				width : 150,
				renderer : function(value) {
					if (value instanceof Date) {
						return new Date(value).format("Y-m-d");
					} else {
						return value;
					}
				},
				dataIndex : "createTime"
			}, {
				id : "logContent",
				header : "日志内容",
				width : 200,
				dataIndex : "content"
			} ],
			autoExpandColumn : "logContent",
			bbar : {
				xtype : "pagination"
			},
			listeners : {
				afterrender : function() {
					this.getStore().load({
						// 回调函数展开所有行
						callback : function() {
							me.expendAll();
						}
					});
				}
			}
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 查询日志事件
	 */
	onSearchLog : function() {
		var me = this;
		me.logGridPanel.getStore().load({
			params : K.serializeJson("#fnLog-search"),
			callback : function() {
				me.expendAll();
			}
		});
	},

	/**
	 * 展开所有行
	 */
	expendAll : function() {
		return;
		var me = this;
		var i;// 循环临时变量
		var arr = [];// 要展开的行的数组
		var store = me.logGridPanel.getStore();
		var storeLength = store.getCount();
		for (i = 0; i < storeLength; i++)// gridpanel的数据源
		{
			var record = store.getAt(i);// 循环遍历每一行
			arr.push(i);
		}
		for ( var j = 0; j < arr.length; j++) {// 遍历数组展开调用toggleRow(index)方法展开某一行
			me.expander.expandRow(arr[j]);
		}
	}
});